<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'foreach.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
       List<String> fruits = new ArrayList<String>();
       fruits.add("apple");
       fruits.add("orange");
       fruits.add("pear");
       fruits.add("watermelon");
       fruits.add("grape");
       fruits.add("banana");
       
       request.setAttribute("fruits", fruits);
     %>
    <!-- forEach标签的用法 -->
    <!-- 用法一：全部遍历 -->
    <c:forEach var="fruit" items="${fruits}">
       <h1><c:out value="${fruit}"></c:out></h1>
    </c:forEach>
    
    ==========================================<br>
    <!-- 用法二：部分遍历 -->
    <c:forEach var="fruit" items="${fruits}" begin="1" end="4">
       <h1><c:out value="${fruit}"></c:out></h1>
    </c:forEach>
    ==========================================<br>
    
    <!-- 用法三：部分遍历并指定步长 -->
    <c:forEach var="fruit" items="${fruits}" begin="1" end="4" step="2">
       <h1><c:out value="${fruit}"></c:out></h1>
    </c:forEach>
    ==========================================<br>
    
    <!-- 用法四：部分遍历并输出元素状态 -->
    <c:forEach var="fruit" items="${fruits}" begin="1" end="4" varStatus="fru">
       <h1><c:out value="${fruit}的四个属性"></c:out></h1>
       <p><c:out value="index属性：${fru.index}"></c:out></p>
       <p><c:out value="count属性：${fru.count}"></c:out></p>
       <p><c:out value="first属性：${fru.first}"></c:out></p>
       <p><c:out value="last属性：${fru.last}"></c:out></p> <br><br>
    </c:forEach>
    ==========================================<br>
  </body>
</html>
